/*
 * @(#)UraConnectorPlugIn.java $version 2010. 8. 11
 *
 * Copyright 2007-2010 NHN Corp. All rights Reserved. 
 * NHN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.nhncorp.usf.core.plugin;

import java.util.Map.Entry;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.w3c.dom.Node;

import com.nhncorp.lucy.common.plugin.AbstractPlugIn;
import com.nhncorp.lucy.common.plugin.PlugInException;
import com.nhncorp.usf.core.config.ConnectorInfo;
import com.nhncorp.usf.core.config.ConnectorInfos;
import com.nhncorp.usf.core.invoker.ConnectorHolder;
import com.nhncorp.usf.core.invoker.InvocationConnectorManager;
import com.nhncorp.usf.core.invoker.SpringInvoker;
import com.nhncorp.usf.core.util.ConnectorUtil;

/**
 * @author Web Platform Development Team
 */
public class UraConnectorPlugIn extends AbstractPlugIn {
    Log log = LogFactory.getLog(UraConnectorPlugIn.class);

    public static final String LUCY_LOCAL_SPRING = "lucyLocalSpring";

    String jmxKey = "objectName";
    String jmxDomain = "USF";

    /**
     * @param node the Node
     * @return boolean the parsing success
     * @throws PlugInException the PlugInException
     */
    @Override
    public boolean build(Node node) throws PlugInException {
        return true;
    }

    /**
     * @throws PlugInException the PlugInException
     */
    @Override
    public void destroy() throws PlugInException {
        InvocationConnectorManager.getInstance().discard();
    }

    /**
     * Configuration에 기술된 Connector(UraInvoker) 초기화.
     *
     * @throws PlugInException the PlugInException
     */
    @Override
    public void init() throws PlugInException {
        try {
            for (Entry<String, ConnectorInfo> connectorInfoEntry : ConnectorInfos.getInstance().getEntrys()) {
                ConnectorInfo connectorInfo = connectorInfoEntry.getValue();

                Object[] protocolInfo = ConnectorUtil.parse(connectorInfo.getAddress());
                ConnectorUtil.UraScheme protocol = (ConnectorUtil.UraScheme) protocolInfo[0];
                ConnectorHolder connectorHolder = null;

                if (protocol.equals(ConnectorUtil.UraScheme.LUCY)) {
                    connectorHolder = new ConnectorHolder(new SpringInvoker());
                }
                
                log.debug("connector " + connectorInfo.getId() + " is created.");
                InvocationConnectorManager.getInstance().putInvoker(connectorInfo.getId(), connectorHolder);
            }

            ConnectorInfos connectorInfos = ConnectorInfos.getInstance();

            // connector 사이즈가 0인 경우도 허용            
            if (connectorInfos.getEntrys().size() == 0) {

                // throw new PlugInException("fail to create connector : connection size = 0 ");
                log.debug("fail to create connector : connection size = 0 ");
            }

        } catch (Exception ex) {
            throw new PlugInException(ex);
        }
    }
}
